 clear all
set mem 500m
set more off

************************************************************************************
clear
use data_working

gen alos=(daysnp+daysfp+dayspb)/admtot
summ alos if admtot>1
** generate per capita beds **
gen popul=ipop/1000
gen bedstot_temps=beds_tot
replace bedstot_temps=0 if beds_tot==1
gen bedsnppub_temps=beds_nppub
replace bedsnppub_temps=0 if beds_nppub==1
gen bedsfp_temps=bdtotfp
replace bedsfp_temps=0 if bdtotfp==1

replace popul=0 if popul==.
gen bdpc=bedstot_temps/(popul+1)
gen bdpc_nppub=bedsnppub_temps/(popul+1)
gen bdpc_fp=bedsfp_temps/(popul+1)

gen admtotpc=admtot/(popul+1)
gen admnppubpc=adm_nppub/(popul+1)
gen admfppc=admtotfp/(popul+1)

summ bdpc if popul>0, det
summ bdpc_nppub if popul>0, det
summ bdpc_fp if popul>0, det

summ admtotpc if popul>0, det
summ admnppubpc if popul>0, det
summ admfppc if popul>0, det


drop if year==1947
bys fcount : gen first_treat=sum(treat_it)
** gen year first treated **
forvalues i=1/1{
	gen yeart`i'=year if treat_it==1 & first_treat==`i'
	by fcounty1: egen yeart`i'2=mean(yeart`i')
	drop yeart`i'
	rename yeart`i'2 yeart`i'
	}

sort fcounty year
by fcounty: gen byte ntreat = sum(treat_it)
sort fcounty ntreat
by fcounty ntreat: gen ntreat1=_n
gen byte first_it = (ntreat==1) & ntreat1==1
xtset fcounty year

* create lags *
gen byte treat_lag=first_it
  forvalues y=1/20  {
	gen byte treat_lag`y' = L`y'.treat_lag 
 	replace treat_lag`y' = 0 if treat_lag`y' ==.
  }

gen byte treat_lag21 = 0
  forvalues y=21/30  {
	replace treat_lag21 = 1 if L`y'.treat_it == 1
  }

* create leads *
  forvalues y=1/9  {
	gen byte treat_lead`y' = F`y'.treat_lag 
 	replace treat_lead`y' = 0 if treat_lead`y' ==.
  }

** Summary stats**
summ yeart if yeart!=., det
summ beds* if year==1953 & yeart==., det
summ admtot adm_npp admtotfp if year==1953 & yeart==., det
gen firmstot=firmsfp+firmsnp+firmspb
gen firms_nppub=firmsnp+firmspb
summ firmstot firms_npp firmsf if year==1953 & yeart==., det
replace daysfp=0 if missing(daysfp)
replace daysnp=0 if missing(daysnp)
replace dayspb=0 if missing(dayspb)

gen daystot=daysfp+daysnp+dayspb
gen days_nppub=daysnp+daysp

forvalues i=1/20 {
	label var treat_lag`i' "`i' Years Later"
}
label var treat_lag21 "21+ Years Later"
label var treat_lag "Year of Funding"

forvalues i=1/9  {
	label var treat_lead`i' "`i' Years Prior"
}

gen ipop2=ipop/100000
replace yeart=3000 if yeart==.

* create MDs per capita *
gen infmdpc=infmd/(popul+1)


*add on earlier years so we can re-create lag structure*
drop if year>1975
append using countyyear_1920_1947
sort fcounty1 year
xtset fcounty1 year

gen treatnew_lag0=0
replace treatnew_lag0=f27.treat_lag

forvalues i=1/21{
	gen treatnew_lag`i'=0
	replace treatnew_lag`i'=f27.treat_lag`i'
}

forvalues i=1/9{
	gen treatnew_lead`i'=0
	replace treatnew_lead`i'=f27.treat_lead`i'
}
gen pop65_pct=ipop65/ipop
gen poplt5_pct=ipoplt5/ipop

label var ipop2 "Popn"
label var imedfaminc "Med. Fam Income"
label var pop65_pct "% Pop 65+"
label var poplt5_pct "% Pop <5"
label var infmdpc "NonFed MDs per Capita"
label var inwpop_pct "% Pop NonWh"


*keep if year==1948
* this will drop 22 county/year observations with large swings (greater than 200 bed diff in dff in beds) and 0 beds as an observation *

gen diff2bd=abs(d2.beds_tot)
egen maxdiff2=max(diff2bd), by(fcounty)


bys fcounty : gen dropvar=1 if maxdiff2>200 & beds_tot==1 & beds_tot[_n-1]>1 & beds_tot[_n+1]>1  & year>=1948
replace dropvar=0 if missing(dropvar)
tab fcounty if dropvar==1

keep if dropvar==0 

xi i.year
xtset fcounty1 year

******************************
* Table 1 Summary Statistics *
******************************

summ daystot days_npp daysf if year==1953 & yeart==., det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if year==1953 & yeart==.,det
summ beds* if year==1953 ,det
summ admtot adm_npp admtotfp if year==1953,det
summ firmstot firms_npp firmsf if year==1953 ,det
summ daystot days_npp daysf if year==1953 ,det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if year==1953 ,det
summ beds* if treat_lag==1,det
summ admtot adm_npp admtotfp if treat_lag==1,det
summ firmstot firms_npp firmsf if treat_lag==1,det
summ daystot days_npp daysf if treat_lag==1,det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if treat_lag==1,det


***************************************************************************
* Coefficients for Figure 4 (left panel), Coefficients listed in Table A4 *
***************************************************************************
areg bdpc treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_noleads_wts, replace label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg bdpc_nppub treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_noleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg bdpc_fp treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_noleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")


** regs of beds pc by ownership type with leads
****************************************************************************
* Coefficients for Figure 4 (right panel), Coefficients listed in Table A7 *
****************************************************************************
areg bdpc treat_lead* treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts, replace label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
test (treat_lead9-treat_lead7)/2=0
test (treat_lag1-treat_lag2)/2=0
test treat_lead9
test treat_lead8, accum
test treat_lead7, accum

areg bdpc_nppub treat_lead*  treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
test (treat_lead9-treat_lead7)/2=0
test (treat_lag1-treat_lag2)/2=0
test treat_lead9
test treat_lead8, accum
test treat_lead7, accum

areg bdpc_fp treat_lead*  treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
test (treat_lead9-treat_lead7)/2=0
test (treat_lag1-treat_lag2)/2=0
test treat_lead9
test treat_lead8, accum
test treat_lead7, accum


** now run with leads on full sample, dropping outlier counties **
areg bdpc treat_lead* treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if fcounty1!=24007&fcounty1!=26125&fcounty1!=34013&fcounty1!=34017&fcounty1!=36119&fcounty1!=39049&fcounty1!=6067&fcounty1!=12103&fcounty1!=53053  [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts_dropoutlier, replace label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg bdpc_nppub treat_lead*  treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if fcounty1!=24007&fcounty1!=26125&fcounty1!=34013&fcounty1!=34017&fcounty1!=36119&fcounty1!=39049&fcounty1!=6067&fcounty1!=12103&fcounty1!=53053  [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts_dropoutlier, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg bdpc_fp treat_lead*  treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if fcounty1!=24007&fcounty1!=26125&fcounty1!=34013&fcounty1!=34017&fcounty1!=36119&fcounty1!=39049&fcounty1!=6067&fcounty1!=12103&fcounty1!=53053 [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lead* treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using bedspc_withleads_wts_dropoutlier, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")


***************************************************************
* Coefficients for Figure 6, Coefficients listed in Table A10 *
***************************************************************
** regs of admissions per capita - just need to create graphs - not tables for current draft
areg admtotpc treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using admspc_noleads_wts, replace label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg admnppubpc treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using admspc_noleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")
areg admfppc treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
   outreg2 treat_lag* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct using admspc_noleads_wts, append label tex se sym(***,**,*) title("Effect of Hill-Burton Treatment (0/1)")

***************************************
* check LOS regression *
** to check if length of stay changes over time **
drop if year<1948
* clean up days variable *
replace daystot=. if daystot==0
replace daysfp=. if daysfp==0
replace days_nppub=. if days_nppub==0

by fcounty: ipolate daystot year, gen (daystot2)
by fcounty: ipolate daysfp year, gen (daysfp2)
by fcounty: ipolate days_nppub year, gen (days_nppub2)

drop daystot daysfp days_nppub 
rename daystot2 daystot
rename daysfp2 daysfp
rename days_nppub2 days_nppub

replace daystot=0 if missing(daystot)
replace days_nppub=0 if missing(days_nppub)
replace daysfp=0 if missing(daysfp)

* clean up admits variable *
replace admtot=. if admtot==1
replace admtotfp=. if admtotfp==1
replace adm_nppub=. if adm_nppub==1

by fcounty: ipolate admtot year, gen (admtot2)
by fcounty: ipolate admtotfp year, gen (admtotfp2)
by fcounty: ipolate adm_nppub year, gen (adm_nppub2)

drop admtot admtotfp adm_nppub
rename admtot2 admtot
rename admtotfp2 admtotfp
rename adm_nppub2 adm_nppub

replace admtot=1 if missing(admtot)
replace adm_nppub=1 if missing(adm_nppub)
replace admtotfp=1 if missing(admtotfp)


gen los_tot=daystot/(admtot)
gen los_nppub=days_nppub/(adm_nppub)
gen los_fp=daysfp/(admtotfp)

summ los*
summ los* [aw=ipop2]

* unweighted *
areg los_tot treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct , absorb(fcounty) cluster(fcounty)
testparm treat_lag*
areg los_nppub treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct , absorb(fcounty) cluster(fcounty)
testparm treat_lag*
areg los_fp treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct , absorb(fcounty) cluster(fcounty)
testparm treat_lag*

* weighted *
areg los_tot treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
testparm treat_lag*
areg los_nppub treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
testparm treat_lag*
areg los_fp treat_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct [aw=ipop2], absorb(fcounty) cluster(fcounty)
testparm treat_lag*
*log close



